package info.rongtong.utils;

import java.util.List;

import info.rongtong.service.inter.ArticleServiceInter;
import info.rongtong.service.inter.CategoryServiceInter;

import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;

public class CheckAndUpdateContextThread implements Runnable {

	private ApplicationContext context = null;
	private ServletContext application = null;
	private CategoryServiceInter categoryServiceImpl = null;
	private ArticleServiceInter articleServiceImpl = null;
	
	public CheckAndUpdateContextThread(ServletContext application){
		this.application=application;
	}
	@SuppressWarnings("unchecked")
	public void run() {
		//不断的根本变量来检查是否有更新
		context = (ApplicationContext) application.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
		categoryServiceImpl = (CategoryServiceInter) context.getBean("categoryServiceImpl");
		articleServiceImpl = (ArticleServiceInter) context.getBean("articleServiceImpl");
		while(true){
			if(VariableUtils.isUpdateArticle){
				System.out.println("文章有更新 ");
				System.out.println(articleServiceImpl);
				//完成操作将状态改为已更新
				VariableUtils.isUpdateArticle = false;
			}
			if(VariableUtils.isUpdateCategory){
				List list= categoryServiceImpl.getCategoryByType(0);
				application.setAttribute("categoryInfoList", list);
				//同上
				VariableUtils.isUpdateCategory = false;
			}
			try {
				Thread.sleep(2000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
